package com.zxtc.wh.servlet;

import com.google.gson.Gson;
import com.zxtc.wh.dao.UserInofDaoImpl;
import com.zxtc.wh.pojo.Result;
import com.zxtc.wh.pojo.UserInofPojo;
import com.zxtc.wh.utils.FileUploadSaveUtil;
import org.apache.commons.fileupload.FileItem;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 用于演示数据库操作的demo服务接口中的上传信息部分
 * 2017年4月13日13:49:42
 */
@WebServlet("/updateInfo")
public class UpdateInfoServlet extends HttpServlet {
    public final static String Encode = "UTF-8";
    public final static String Decode = "UTF-8";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);
        UpdateInfoRusult rusult = new UpdateInfoRusult();

        req.setCharacterEncoding(Decode);
        resp.setContentType("text/html;charset=utf-8");

//        byte[] con = {110, 97, 109, 101, -28, -72, -83, -26, -106, -121};
//        String s1 = new String(con, "US-ASCII");
//        String s2 = new String(con, "ISO-8859-1");
//        String s3 = new String(con, "UTF-8");
//        String s4 = new String(con, "UTF-16BE");
//        String s5 = new String(con, "UTF-16LE");
//        String s6 = new String(con, "UTF-16");
//        req.getInputStream()
        Map<String, String[]> parameterMap = req.getParameterMap();
        String description1 = req.getParameter("description");
//        ServletInputStream inputStream = req.getInputStream();
        BufferedReader reader = req.getReader();

        UserInofPojo userInofPojo = new UserInofPojo();

        try {
            Map<String, List<FileItem>> map = FileUploadSaveUtil.getFileParameterMap(req);

            List<FileItem> descriptionList = map.get("description");
            String description = descriptionList.size() >= 1 ? descriptionList.get(0).getString(req.getCharacterEncoding()) : null;
            userInofPojo.setRemark(description);
            List<FileItem> nameList = map.get("name");
            String name = nameList.size() >= 1 ? nameList.get(0).getString(req.getCharacterEncoding()) : null;
            userInofPojo.setUserName(name);

            List<FileItem> fileParameter = map.get("image");
            for (FileItem fileItem : fileParameter) {
                //获取保存文件资源的相对路径
                String imageFileName = FileUploadSaveUtil.saveRandomName(this.getServletContext(), fileItem);

                userInofPojo.setUserPhotPath(FileUploadSaveUtil.getFileName(imageFileName));
                new UserInofDaoImpl().insert(userInofPojo);
            }

            rusult.responseCode = 200;
            rusult.message = "OK";

        } catch (Exception e) {
            e.printStackTrace();
            rusult.responseCode = 400;
            rusult.message = e.toString();
        }
//        File image = req.getParameter("image");

        Gson gson = new Gson();
        resp.getWriter().println(gson.toJson(rusult));


    }

    private static class UpdateInfoRusult extends Result<Void> {
    }
}
